package mutsumimod.actions.deprecated;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import mutsumimod.actions.CopyChooseOneAction;
import mutsumimod.cards.copy.ONeiGai;
import mutsumimod.cards.deprecated.ONeiGaiUtil;

import java.util.ArrayList;

@Deprecated
public class ONeiGaiAction extends AbstractGameAction {
    private AbstractPlayer p;
    public ONeiGaiUtil copyCard;
    private ArrayList<AbstractCard> removeCards = new ArrayList<>();

    public ONeiGaiAction(boolean isUpgrade) {
        this.actionType = ActionType.DRAW;
        this.duration = 0.25F;
        this.p = AbstractDungeon.player;
        this.copyCard = new ONeiGaiUtil();
        if(isUpgrade){
            this.copyCard.upgrade();
        }
    }

    public void update() {
        if (this.duration == Settings.ACTION_DUR_FAST) {
            for(AbstractCard c : this.p.hand.group) {
                if (shouldRemove(c)) {
                    this.removeCards.add(c);
                }
            }

            this.p.hand.group.removeAll(this.removeCards);

            if (p.hand.group.isEmpty()) {
                this.returnCards();
                this.isDone = true;
                return;
            }

            if (p.hand.group.size() == 1) {
                for(AbstractCard c : this.p.hand.group) {
                    copyCard.anotherCard=c;
                    addToTop(new CopyChooseOneAction(copyCard));
                }
                this.returnCards();
                this.isDone = true;
                return;
            }

            if (p.hand.group.size() > 1) {
                AbstractDungeon.handCardSelectScreen.open("选择一张手牌", 1, false, false, false, false);
                this.tickDuration();
                return;
            }
        }

        if (!AbstractDungeon.handCardSelectScreen.wereCardsRetrieved) {
            for(AbstractCard c : AbstractDungeon.handCardSelectScreen.selectedCards.group) {
                copyCard.anotherCard=c;
                addToTop(new CopyChooseOneAction(copyCard));
            }

            AbstractDungeon.handCardSelectScreen.wereCardsRetrieved = true;
            AbstractDungeon.handCardSelectScreen.selectedCards.group.clear();
            this.returnCards();
            this.isDone = true;
        }

        this.tickDuration();
    }

    public boolean shouldRemove(AbstractCard c) {
        return c instanceof ONeiGai;
    }

    private void returnCards() {
        for(AbstractCard c : removeCards) {
            this.p.hand.addToTop(c);
        }
        this.p.hand.refreshHandLayout();
    }


}
